﻿using System;
using System.Text;
using Microsoft.Practices.RecipeFramework;
using SMC.SPALM.Types;
using Microsoft.Practices.Common;

namespace SteriaMummert.SharePoint.SoftwareFactoryLite.ValueProviders
{
    //provides an empty array of a given type
    public class DefaultCollectionProvider : ValueProvider, IAttributesConfigurable
    {
        public override Microsoft.Practices.Common.Services.ValueInfo Argument
        {
            get
            {
                return base.Argument;
            }
        }

        #region IAttributesConfigurable Members

        void IAttributesConfigurable.Configure(System.Collections.Specialized.StringDictionary attributes)
        {
            if (attributes["ObjectType"] != null)
            {
                ObjectType = attributes["ObjectType"];
            }
        }

        #endregion

        private string _ObjectType = "";
        public string ObjectType
        {
            get { return _ObjectType; }
            set { _ObjectType = value; }
        }

        public override bool OnBeginRecipe(object currentValue, out object newValue)
        {
            if (currentValue != null)
            {                
                newValue = null;
                return false;
            }

            if (_ObjectType != "")
            {
                //create array instance of type _ObjectType
                Type t = Type.GetType(_ObjectType);
                newValue = System.Array.CreateInstance(t, 0);
                return true;
            }
            else
            {
                newValue = null;
                return false;
            }
        }
    }
}
